package com.tools.cleanmaster.ui.view.trash;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

import com.tools.cleanmaster.R;

import java.util.List;


@SuppressWarnings("static-access")
class TrashCleanChildExpandableListView extends ExpandableListView
        implements OnGroupExpandListener, OnGroupCollapseListener {

    private final int mSecondItemHeight;
    private final int mThirdItemViewHeight;
    private final int mDeviderHeight;

    public static class TrashCleanChildExpandableListAdapter extends TrashCleanListAdapter {
        final TrashCleanChildExpandableListView mListView;

        public void setFirstInParent(boolean isFirst) {
            this.mIsFirstInParent = isFirst;
        }

        private boolean mIsFirstInParent = false;

        TrashCleanChildExpandableListAdapter(Context context, List<TrashGroup> trashGroups, TrashScanListener listener,
                                             TrashCleanListAdapter parent, TrashCleanChildExpandableListView listView) {
            super(context, trashGroups, listener, parent, null, listView);
            mGroupItemLayoutRes = R.layout.trash_clean_second_level_item;
            mChildItemLayoutRes = R.layout.trash_clean_child_item;
            mListView = listView;
            registerDataSetObserver(new DataSetObserver() {

                @Override
                public void onChanged() {
                    if (mListView.isGroupExpanded(0)) {
                        mListView.onGroupExpand(0);
                    }
                }

            });
        }

        @Override
        public int getChildType(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public int getChildTypeCount() {
            return 1;
        }

        @Override
        ItemViewHolder onCreateGroupViewHolder(View convertView) {
            ItemViewHolder holder = super.onCreateGroupViewHolder(convertView);
            return holder;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            View view = super.getGroupView(groupPosition, isExpanded, convertView, parent);
            ItemViewHolder holder = (ItemViewHolder) view.getTag();
            holder.mDividerHead.setVisibility(View.VISIBLE);
            holder.mDividerEnd.setVisibility(View.GONE);
            if (mListView.isGroupExpanded(groupPosition)) {
                holder.mDividerEnd.setVisibility(View.VISIBLE);
            } else {
                holder.mDividerEnd.setVisibility(View.GONE);
            }
            if (mIsFirstInParent) {
                holder.mDividerHead.setVisibility(GONE);
            }
            if (mGroupItemLayoutRes == R.layout.trash_clean_second_level_item) {
                TrashGroup group = mTrashGroups.get(groupPosition);
            }
            return view;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                                 ViewGroup parent) {
            View view = onPrepareChildView(groupPosition, childPosition, isLastChild, convertView, parent);
            return view;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChangedOnly();
        }

    }

    TrashCleanChildExpandableListView(Context context) {
        super(context, null, R.style.MyWidget_ExpandableListView_ForTrashClean);
        setSelector(android.R.color.transparent);
        mSecondItemHeight = context.getResources().getDimensionPixelSize(R.dimen.list_item_height_second);

        mDeviderHeight = context.getResources().getDimensionPixelSize(R.dimen.trash_clean_list_divider_height);
        View view = LayoutInflater.from(context).inflate(R.layout.trash_clean_child_item, null);
        view.measure(0, 0);
        mThirdItemViewHeight = view.getMeasuredHeight();
        setOnGroupCollapseListener(this);
        setOnGroupExpandListener(this);
    }

    @Override
    public void onGroupCollapse(int groupPosition) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
            TrashCleanListAdapter adapter = (TrashCleanListAdapter) getExpandableListAdapter();
            TrashGroup group = adapter.getTrashGroups().get(groupPosition);
            if (group instanceof TrashGroupWithIndicator) {
                ((TrashGroupWithIndicator) group).mIsExpand = false;
            }
        }
        ViewGroup.LayoutParams lp = getLayoutParams();
        if (lp == null) {
            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        lp.height = LayoutParams.WRAP_CONTENT;
        setLayoutParams(lp);
    }

    @Override
    public void onGroupExpand(int groupPosition) {
        TrashCleanListAdapter adapter = (TrashCleanListAdapter) getExpandableListAdapter();
        int childCount = adapter.getTrashGroups().get(0).getChildItems().size();
        ViewGroup.LayoutParams lp = getLayoutParams();
        if (lp == null) {
            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        lp.height = mSecondItemHeight + childCount * mThirdItemViewHeight;
        setLayoutParams(lp);
    }

}
